<template>
  <!--实体模型-->
  <basic-container>
    <div>
      <m-s-search @searchClick="v=>{queryForm=v,page.currentPage=1,getList()}"
                  @oc="show=!show"
                  :list="EntityModelOption.column">
      </m-s-search>
    </div>
    <avue-crud :option="EntityModelOption"
               :table-loading="loading"
               :data="data"
               @selection-change="v=>selection=v"
               @current-change="currentChange"
               @size-change="sizeChange"
               ref="crud"
               :key="show?'a':'b'"
               :page.sync="page">
      <template slot="menuLeft" slot-scope="{size}">
        <el-button :size="size" type="primary"
                   @click="addToTabsList('实体模型',{oid:-1},'modelRelationTemp','open')">
          创建
        </el-button>
        <el-button :size="size" type="danger" :disabled="selection.length===0"
                   @click="remove(selection)">删除
        </el-button>
        <el-button :size="size" @click="release()" :disabled="selection.length!==1">发布</el-button>

        <!-- <el-button :size="size" :disabled="selection.length===0" @click="checkout()">检出</el-button> -->

        <el-button :size="size" @click="discard(selection)" :disabled="selection.length===0">作废</el-button>
      </template>

      <template slot="nameCn" slot-scope="{row}">
        <span class="column-btn" @click="addToTabsList(row.nameCn+'实体模型',row,'modelRelationTemp','view')">
          {{ row.nameCn }}
        </span>
      </template>

      <template slot="menu" slot-scope="{row}">
        <el-button type="text" @click="addToTabsList(row.nameCn+'实体模型',row,'modelRelationTemp','view')">
          查看详情
        </el-button>
        <el-button :disabled="row.state===2?true:row.state===1" type="text"
                   @click="addToTabsList(row.nameCn+'实体模型',row,'modelRelationTemp','edit','jc')">
          修改
        </el-button>
        <el-button :disabled="row.state===2?true:(row.state!==1||row.newFlag!==1)" type="text"
                   @click="addToTabsList(row.nameCn+'实体模型',row,'modelRelationTemp','edit','xd')">
          修订
        </el-button>
        <el-button :disabled="row.state===2?true:row.checkState===1" type="text" @click="checkin(row)">
          检入
        </el-button>
        <el-button :disabled="row.state===2?true:row.checkState===1" type="text" @click="revokeCheckout(row)">
          撤销检出
        </el-button>
        <el-button type="text"
                   @click="addToTabsList(row.nameCn+'实体模型历史',
                   {...row,...other__,...{params_:{masterOid:row.masterOid}}},
                   'commonHistory','show')">
          历史
        </el-button>
      </template>
    </avue-crud>

    <common-entity-dialog ref="ced" @refresh="getList()"></common-entity-dialog>

    <ms-drawer @refresh="getList()" :visible.sync="visible" :tab-list.sync="tabsList"></ms-drawer>
  </basic-container>
</template>
<script>
import MSSearch from "@/DMDComponents/MSSearch.vue";
import CommonEntityDialog from "@/views/DMDComponent/commonEntityDialog.vue";

import * as api from "@/api/modulManage/entity"
import * as common from "@/api/common";

export default {
  name: "EntityModel",
  components: {CommonEntityDialog, MSSearch},
  provide() {
    return {
      temp: "1",
      parentIdList: () => this.parentIdList,
      modelTypeList: () => this.modelTypeList
    }
  },
  inject: ["EntityModelOption"],
  data() {
    return {
      show: false,

      queryForm: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 1
      },
      data: [],
      loading: false,

      parentIdList: [],
      modelTypeList: [],

      selection: [],

      tabsList: [],
      visible: false,

      other__: {
        url: '/api/manager-api/mx-type/historyPage',
        option: this.EntityModelOption,
        method: "get",
        requestDataType: "params",
      }
    }
  },
  methods: {
    revokeCheckout(row) {
      this.$confirm("是否撤销检出").then(() => {
        api.revokeCheckout({id: row.oid}).then(() => {
          this.$message.success("撤销检出成功");
          this.getList();
        })
      })
    },
    checkin(row) {
      this.$confirm("是否检入").then(() => {
        api.checkin({id: row.oid}).then(() => {
          this.$message.success("检入成功")
          this.getList();
        })
      })
    },
    // 向抽屉中添加tab页 根据 oid 去重
    addToTabsList(title, row, component, type, btn) {
      console.log('rowrowrowrow',row)
      /*if ("edit" === type) {
          let method = btn === "jc" ? "checkout" : "revise";
          api[method]({id: row.oid}).then((res) => {
              if (btn === "jc") {
                  this.addOrEdit(title, res.data.data, component, type);
                  this.selection = [];
              }
              this.getList();
          })
      } else {
          this.addOrEdit(title, {...row,btn}, component, type);
      }*/
      this.addOrEdit(title, {...row, btn}, component, type);

    },
    addOrEdit(title, row, component, type) {
      let ids = this.tabsList.map(item => (item.row.oid + "-" + item.component));
      if (!ids.includes((row.oid + "-" + component))) {
        this.tabsList.push({title, row, component, type})
      }
      this.visible = true;
    },
    // 删除
    remove(data) {
      this.$confirm("是否删除？").then(() => {
        let ids = data.map(item => item.oid);
        api.del({ids: ids.join(",")}).then(() => {
          this.$message.success("删除成功");
          this.getList();
          this.selection = [];
        })
      })
    },
    discard(data) {
      this.$confirm("是否作废？").then(() => {
        let ids = data.map(item => item.oid);
        if (ids.length > 1) {
          return this.$message.warning("一次只能作废一条数据！")
        }
        api.discard({id: ids.join(",")}).then(() => {
          this.$message.success("作废成功");
          this.getList();
          this.selection = [];
        })
      })
    },
    checkout(oid) {
      api.checkout({oid: oid}).then((res) => {
        this.$message.success("检出成功");
        this.getList();
        this.selection = [];
      })
    },
    release() {
      api.publish({id: this.selection[0].oid}).then(() => {
        this.$message.success("发布成功");
        this.getList();
        this.selection = [];
      })
    },

    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      this.getList();
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      this.getList();
    },
    getList() {
      api.getQueryEntity({
        current: this.page.currentPage,
        size: this.page.pageSize, ...this.queryForm
      }).then(res => {
        const {list, totalRows} = res.data.data;
        this.page.total = totalRows;
        this.data = list;

        this.getEntityList();
      })
    },

    // 获取 父模型
    getEntityList() {
      api.getEntity().then(res => {
        this.parentIdList = res.data.data;
      })
    },
    //获取 模型类型
    getModelType() {
      // defaultFlag = 1 两个都可选。defaultFlag!==1 只能选业务模型，
      common.getDic("modelType").then(res => {
        if (!res.data.data) {
          return;
        }
        const defaultFlag = localStorage.getItem("defaultFlag")
        if (defaultFlag === '1') {
          this.modelTypeList = res.data.data;
        } else {
          this.modelTypeList = res.data.data.filter(v => v.value === '1');
        }
      })
    },
  },
  mounted() {
    this.getList();
    const defaultAppid = localStorage.getItem("appId");
  },
  created() {
    this.getEntityList();
    this.getModelType();
  },
}
</script>

<style scoped lang="scss">

</style>
